#include "formulafactorytest.h"

bool FormulaFactoryTest::run() {
    short count=0;

    if (!FormulaFactoryTest::getRealRandomNumberTest())
        std::cout << "ERROR " << ++count << " FormulaFactoryTest::getRealRandomNumberTest() faild!" << std::endl;
    if (!FormulaFactoryTest::createNewFormulaTest())
        std::cout << "ERROR " << ++count << " FormulaFactoryTest::createNewFormulaTest() faild!" << std::endl;
    if (!FormulaFactoryTest::simpleRecombinationTest())
        std::cout << "ERROR " << ++count << " FormulaFactoryTest::simpleRecombinationTest() faild!" << std::endl;
    if (!FormulaFactoryTest::copyFirstFormulaToTheEndOfTheSecondTest())
        std::cout << "ERROR " << ++count << " FormulaFactoryTest::copyFirstFormulaToTheEndOfTheSecondTest() faild!" << std::endl;

    return count==0;
}

bool FormulaFactoryTest::getRealRandomNumberTest() {
    assert(RAND_MAX==2147483647);
    assert((-32 < ((rand()>>25)-31+((double)(rand()>>18))/(8191.0))) && (((rand()>>25)-31+((double)(rand()>>18))/(8191.0)) < 33));
    assert((RAND_MAX >> 25)==63);
    assert((RAND_MAX >> 28)==7);
    //    std::cout << (RAND_MAX>>28);
    return true;
}

bool FormulaFactoryTest::createNewFormulaTest() {
    unsigned int size=11;
    Formula *testFormula = FormulaFactory::createNewFormula(size, false);
    std::string& output=testFormula->toString();
//    std::cout << output << std::endl;
    LinkedList<FormulaElement>& tempList = testFormula->getListOfFormulaElements();
    assert(!(tempList.getSize()-size));
    delete &output;
    delete testFormula;
    return true;
}

bool FormulaFactoryTest::simpleRecombinationTest() {
    unsigned int size=11;
    Formula* f1;
    Formula* f2;
    Formula* f3;
    f1=FormulaFactory::createNewFormula(size, false);
    f2=FormulaFactory::createNewFormula(size, false);
    f3=FormulaFactory::simpleRecombination(*f1, *f2);
    assert(f3->getListOfFormulaElements().getSize()==25);
    Input* testInput = new Input();
    testInput->createPrimeInput(5);
    InputIterator& inputIterator=testInput->getNewIterator();
    f3->evaluate(inputIterator);
    delete f1;
    delete f2;
    delete f3;
    delete testInput;
    delete &inputIterator;
    return true;
}

bool FormulaFactoryTest::copyFirstFormulaToTheEndOfTheSecondTest() {
    unsigned int size=11;
    Formula* f1;
    Formula* f2;
    f1=FormulaFactory::createNewFormula(size, false);
    f2=FormulaFactory::createNewFormula(size, false);
    FormulaFactory::copyFirstFormulaToTheEndOfTheSecond(*f1, *f2);
    assert(f1->getListOfFormulaElements().getSize()==11);
    assert(f2->getListOfFormulaElements().getSize()==22);
    delete f1;
    delete f2;
    return true;
}
